package com.sfac.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.sfac.interceptor.ViewMappingInterceptor;
import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
// 设置配置生效时期（在WebMvcAutoConfiguration配置生效后生效即与该配置重复则覆盖前一个配置内容覆盖）
@AutoConfigureAfter({WebMvcAutoConfiguration.class})
public class WebMvcConfig implements WebMvcConfigurer {

    @Value("${server.http.port}")
    private int httpPort;
    @Autowired
    private ResourceConfigBean resourceConfigBean;
    @Autowired
    private ViewMappingInterceptor viewMappingInterceptor;

    /**
     * 注册拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(viewMappingInterceptor).addPathPatterns("/**");

    }



    /**
     * 配置http请求的连接
     * @return
     */
    @Bean
    public Connector connector(){
        Connector connector = new Connector();
        connector.setPort(httpPort);
        connector.setScheme("http");
        return connector;
    }


    /**
     * - 重新注册 ServletWebServerFactory Bean
     * - 以 TomcatServletWebServerFactory 实现，添加 Http 连接器
     */
    @Bean
    public ServletWebServerFactory servletWebServerFactory(){
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        factory.addAdditionalTomcatConnectors(connector());
        return factory;
    }



    /**
     * 添加资源映射
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 获取系统名字
        String osName = System.getProperty("os.name");
        if (osName.toLowerCase().startsWith("win")) {
            registry.addResourceHandler(resourceConfigBean.getResourcePattern())
                    .addResourceLocations(ResourceUtils.FILE_URL_PREFIX + resourceConfigBean.getLocalPathForWin());
        } else  {
            registry.addResourceHandler(resourceConfigBean.getResourcePattern())
                    .addResourceLocations(ResourceUtils.FILE_URL_PREFIX + resourceConfigBean.getLocalPathForLin());
        }


//            registry.addResourceHandler("/static/**").addResourceLocations("/static/");
//        registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.FILE_URL_PREFIX+"D:\\imgs");

    }


    /**
     * 配置跨域请求(协议，域名，端口只要一个不同就代表是跨域请求)
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("POST","GET","PUT","DELETE","OPTIONS")
                .maxAge(3600)
                .allowedOriginPatterns("*");
        //      .allowCredentials(true); 携带cookie
    }
}
